package com.smm.config.shiro;

import com.smm.config.JWTToken;
import com.smm.entity.User;
import com.smm.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ByteSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;


public class UserRealm extends AuthorizingRealm {
    private final Logger log = LoggerFactory.getLogger(UserRealm.class);
    @Resource
    private UserService userService;

    @Override
    public void setName(String name) {
        super.setName("userRealm");
    }

    @Override
    public boolean supports(AuthenticationToken token) {
        log.info("userRealm.supports 方法 ==>"+String.valueOf(token instanceof UsernamePasswordToken));
        //System.out.println(token instanceof UsernamePasswordToken);
        //return true;
        return token instanceof UsernamePasswordToken;
    }

    /**
     * 重写授权方法，决定了用户登录后的权限，能执行哪些方法，不能执行哪些方法
     * @param principals
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        log.info("执行了=>获取授权信息");

        String token = String.valueOf(principals);
        log.info(token);

        //User currentUser =(User) SecurityUtils.getSubject().getPrincipal();
        //User user = (User) principals.getPrimaryPrincipal();
        //log.info(String.valueOf(user));
        //log.info(String.valueOf(currentUser));
        //Subject subject = SecurityUtils.getSubject();
        //User user1 = (User) subject.getPrincipal();
        //log.info(String.valueOf(user1));
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();

        //info.addRole(user.getRole());

        return info;
    }

    /**
     * 认证方法，判断账户是否合法,用户名和密码是否和数据库一致
     * @param token
     * @return
     * @throws AuthenticationException
     */

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        log.info("执行了=>获取认证信息");
        //获取usernamepassword
        //log.info((String) token.getCredentials());
        //UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) token;
        Object principal = token.getPrincipal();
        Object credentials = token.getCredentials();
        //System.out.println(""+principal);
        log.info("username===>"+principal);
        //System.out.println(credentials);
        log.info("password===>" + credentials);
        String username = (String) principal;
        //String password = new String(usernamePasswordToken.getPassword());
        //从数据库中获取对应用户信息
        User user = new User();
        user.setUsername(username);
        User findUser = userService.findUser(user);
        //System.out.println(findUser);
//        if (!findUser.getStatus()) {
//            throw new LockedAccountException();
//        }
        //验证用户密码
        return new SimpleAuthenticationInfo(
                findUser,
                findUser.getPassword(),//数据库中获取的密码
                ByteSource.Util.bytes(findUser.getUsername()),//加盐
                this.getName());

    }
}
